/**
 * Created by jade on 2016/3/1.
 */
define(function (require, exports, module) {

    //公共验证规则

    var Validator = require("validator");
    var email = Validator.getRule("email");
    var mobile = Validator.getRule("mobile");
    var number = Validator.getRule("number");
    var required = Validator.getRule("required");
    var maxlength = Validator.getRule("maxlength");



    var isSpecialSymbol = /^[\u4e00-\u9fa5]+$/;
    //验证用户名
    Validator.addRule("isSpecialSymbol",isSpecialSymbol, "对不起，请勿输入特殊字符!");



    //验证数字0-99
    var normalNumber=/^([0-9]{1,2})$/;
    Validator.addRule("normalNumber", function (options) {
        var element = options.element;
        var val = element.val();
        return normalNumber.test(val);
    }, '请提供0-99范围内的整数!');



    var userName = /^[a-zA-Z]\w{3,20}$/;
    var phone = /^((0[0-9]{2,3})-?)([0-9]{7,8})(-?([0-9]{3,6}))?$/;
    var numberOrLetter=/^[A-Za-z0-9]*$/;
    var numberOrLetterOrUnderline=/^[a-zA-Z0-9_]*$/;
    var mobiles=/^1[7358]\d{9}(?:\,1[358]\d{9})*$/;//多个手机号码
    var realJson=/^(([A-Za-z0-9]+\:[A-Za-z0-9]+){1}(\,[A-Za-z0-9]+\:[A-Za-z0-9]+)+|([A-Za-z0-9]+\:[A-Za-z0-9]+){1})$/;
    var json=/[^\"\\\n\r]*\"|true|false|null|-?\\d+(?:\\.\\d*)?(?:[eE][+\\-]?\\d+)?/;
    var positiveNumber = /^[1-9]+\d*$/;
    Validator.addRule("json",json,"请输入JSON格式");
    Validator.addRule("realJson",realJson,"请输入正确的格式,例:xxxx:xxx,xxxx:xxxx");
    Validator.addRule("mobiles",mobiles,"请输入正确的手机号码格式");



    //验证字母或数字
    Validator.addRule("numberOrLetter",numberOrLetter,"请输入字母或数字");
    //验证字母或数字或下划线
    Validator.addRule("numberOrLetterOrUnderline",numberOrLetterOrUnderline,"请输入字母或数字或下划线");
    //用户名
    Validator.addRule("userName", userName, "用户名格式不正确");

    //验证正整数
    Validator.addRule("positiveNumber", positiveNumber, "输入的排序次序格式不正确");

    Validator.addRule("startLetter",/^[a-zA-Z]/,"{{display}}必须以字母开头");
    var emailormobile = email.or("mobile");

    //手机或邮箱
    Validator.addRule("emailormobile", emailormobile, '{{display}}格式不正确');

    //手机或邮箱/或用户名
    var emailormobileoruserName = Validator.getRule("emailormobile").or(Validator.getRule("userName"));

    Validator.addRule("emailormobileoruserName", emailormobileoruserName, '{{display}}格式不正确');

    //检查是否是主用户
    Validator.addRule("checkIsPrimaryUser", function (options, commit) {
        var element = options.element;
        var val=element.val();
        $.ajax({
            url: "/navconfig/checkIsPrimaryUser",
            type: "post",
            data: {owner:val}
        }).done(function (data) {
            commit(data.status,  options.errorMsg);
        }).fail(function(){
            commit(false,"请求异常");
        })
    })
    module.exports = Validator;
})